<?php
include_once(PATH_APP_COMMON . "AuthManager.php");
abstract class Controller_AdminList extends Controller{
	
	/**
	 * 在执行actionIndex之前设置index的condition数组
	 * @param &array $conditions
	 */
	protected function _setIndexCondition(&$conditions){
		
	}
	
	function actionIndex()
	{
		
		$limit = _gp('limit','int') ? _gp('limit','int') : C('PAGESIZE');
		$sort = _gp("sort","string") ? _gp("sort","string") : $this->model->getPkName();
		$dir = _gp("dir","string") ? _gp("dir","string") : "desc";
		
		$conditions = array();		
		$message = "";

		$this->_setIndexCondition($conditions, $message);
   		
		$this->setBack();

    	if (!empty($conditions)) {
    		$sql_where .= ' and ' . join(" and ", $conditions);
    	}

		$total = $this->selectCount("where 1 $sql_where");
		$pager = new Helper_Pager($total, $limit, _gp('page', 'int'));
		$pager_min = $pager->minimum;

		$data = $this->select("*", "where 1 $sql_where order by `$sort` $dir limit $pager_min,$limit");
		
		$this->assign('pager',$pager->display);
		$this->assign('message',$message);
		$this->assign('data',$data);
		$this->assign('pk',$this->model->getPkName());

		$this->display($this->_tplForIndex);

	}

	function actionCreate()
	{
		$pk = $this->getPkName();
		$this->assign("pk",$pk);
		$this->setBack();
		$this->display();
	}
	
	function actionEdit(){
		$pk = $this->model->getPkName();
		$pkv = _gp($pk,'int');
		$data = $this->be($pkv);
		
		$this->assign("pk",$pk);
		$this->assign("data",$data);
		$this->display($this->_tplForEdit);
	}
	
	function actionSearch(){
		
		$pk = $this->model->getPkName();
		$this->assign("pk",$pk);
		
		$this->display($this->_tplForSearch);
	}
	
	function actionSave(){
		$this->save($_POST);
		$this->success(MESSAGE_SUCCESS);
	}

	function actionRemove()
	{
		$pk = $this->model->getPkName();
		$pkv = _gp($pk,'int');
		$this->remove($pkv);
		$this->success(MESSAGE_SUCCESS);
	}
	
	function actionBatch(){
		$data = $_POST['data'];
		$do = strtolower(_gp("do",'string'));
		$pkvs = _gp("pkv", 'array');
		$pk = $this->getPkName();
		switch ($do){
			case "order":
				foreach ($data as $id => $row){
					$row[$pk] = $id;
					$this->save($row);
				}
				break;
			case "remove":
				$this->remove($pkvs);
			break;
		}
		$this->success(MESSAGE_SUCCESS);
	}
}
